﻿using SharpDX.DirectInput;
using TableTool.Model;
using TlToolBase;

namespace TableTool
{
    /// <summary>
    /// 编辑
    /// </summary>
    public partial class LookModelEdit : Form
    {
        bool flag = true;
        List<Key> downKeys = new List<Key>();
        public event Action FromColse;
        public LookModelEdit()
        {
            InitializeComponent();
            DoubleBuffered = true;
            this.Load += LookModelEdit_Load;
            this.FormClosing += LookModelEdit_FormClosing;
        }

        private void LookModelEdit_FormClosing(object? sender, FormClosingEventArgs e)
        {
            FromColse?.Invoke();
        }

        private void LookModelEdit_Load(object? sender, EventArgs e)
        {
            KeyLook();
            Ini();
        }
        private void Ini()
        {
            panel1.Controls.Clear();
            int y = 0;
            var DataItems = ElsTool.usdata.dataItems;
            foreach (var di in DataItems)
            {
                udLookModelEdit uc = new udLookModelEdit()
                {
                    lookModel = di,
                };
                uc.Edit += Uc_Edit;
                uc.Delete += Uc_Delete;
                panel1.Controls.Add(uc);
                uc.Location = new Point(1, y);
                y += uc.Height;
            }

        }

        private void Uc_Delete(LookModel obj)
        {
            var DataItems = ElsTool.usdata.dataItems;
            foreach (var item in DataItems)
            {
                if (item.groupName == obj.groupName && item.Name == obj.Name)
                {
                    DataItems.Remove(item);
                    break;
                }
            }
            Ini();
        }

        private void Uc_Edit(LookModel obj)
        {
            textBox1.Text = obj.groupName;
            textBox2.Text = obj.Name;
            textBox3.Text = obj.timeSecond.ToString();
        }

        private void KeyLook()
        {
            var directInput = new DirectInput();
            var Devices = directInput.GetDevices(DeviceType.Keyboard, DeviceEnumerationFlags.AllDevices);
            foreach (var item in Devices)
            {
                if (item.Type == DeviceType.Keyboard)
                {
                    var mykey = new SharpDX.DirectInput.Keyboard(directInput);
                    mykey.Acquire();
                    Task.Factory.StartNew(() =>
                    {
                        while (flag)
                        {
                            KeyboardState kstate = mykey.GetCurrentState();
                            if (kstate != null)
                            {
                                string value = "";
                                foreach (var item in kstate.PressedKeys)
                                {
                                    if (downKeys.Count > 0)
                                    {
                                        if (downKeys[downKeys.Count - 1] != item)
                                        {
                                            downKeys.Add(item);
                                        }
                                    }
                                    else
                                    {
                                        downKeys.Add(item);
                                    }
                                }
                                string downkeys = "";
                                foreach (var item1 in downKeys)
                                {
                                    downkeys += item1.ToString() + " ";
                                }
                                SetLable(label2, downkeys);
                                Thread.Sleep(100);
                            }
                        }
                    });
                }
            }
        }
        private void SetLable(Label lb, string value)
        {
            if (lb.IsHandleCreated == false)
            {
                return;
            }
            try
            {
                if (lb.InvokeRequired)
                {
                    lb.Invoke(new Action<Label, string>(SetLable), lb, value);
                }
                else
                {
                    lb.Text = value;
                }
            }
            catch
            {

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox1.Text) || string.IsNullOrEmpty(textBox2.Text))
            {
                return;
            }
            int.TryParse(textBox3.Text, out int span);
            var DataItems = ElsTool.usdata.dataItems;

            var ud = DataItems.Find(x => x.groupName == textBox1.Text && x.Name == textBox2.Text);
            if (ud == null)
            {
                ud = new LookModel()
                {
                    groupName = textBox1.Text,
                    Name = textBox2.Text,
                    timeSecond = span,
                    guid=Guid.NewGuid(),
                };
                ud.Keys.AddRange(downKeys);
                DataItems.Add(ud);
            }
            else
            {
                ud.timeSecond= span;
                ud.Keys.Clear();
                ud.Keys.AddRange(downKeys);
            }
            Ini();
            button3_Click(null, null);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            downKeys.Clear();
            string downkeys = "";
            foreach (var item1 in downKeys)
            {
                downkeys += item1.ToString() + " ";
            }
            SetLable(label2, downkeys);
        }


    }
}
